package com.yl.ylnews;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.yl.ylnews.utils.PrefUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/17 0017.
 */

public class GuideActivity extends Activity {
    private android.support.v4.view.ViewPager vppager;
    private android.widget.Button btnstart;
    private android.widget.LinearLayout lldots;
    private android.widget.ImageView ivreddot;
    private int [] imagesID = {R.mipmap.guide_1,R.mipmap.guide_2,R.mipmap.guide_3};
    private ArrayList<ImageView> imageViews;
    private int mRedDis;//红点移动的距离

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        this.ivreddot = (ImageView) findViewById(R.id.iv_reddot);
        this.lldots = (LinearLayout) findViewById(R.id.ll_dots);
        this.btnstart = (Button) findViewById(R.id.btn_start);
        this.vppager = (ViewPager) findViewById(R.id.vp_pager);
        initData();
        vppager.setAdapter(new GuideViewPagerAdapter());

        //设置布局树状图加载监听
        ivreddot.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {



            @Override
            public void onGlobalLayout() {
                //防止重复监听,移除回调
                ivreddot.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                mRedDis = lldots.getChildAt(1).getLeft() - lldots.getChildAt(0).getLeft();
            }
        });

        //监听viewpager滑动
        vppager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ivreddot.getLayoutParams();

                layoutParams.leftMargin = (int) (positionOffset * mRedDis + position * mRedDis);
                ivreddot.setLayoutParams(layoutParams);

            }

            @Override
            public void onPageSelected(int position) {
                if (position == imagesID.length - 1) {
                    btnstart.setVisibility(View.VISIBLE);
                } else {
                    btnstart.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        btnstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PrefUtil.setSharedPerenceBoolean(GuideActivity.this,"is first enter",false);
                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void initData() {
        imageViews = new ArrayList<>();
        for (int i = 0; i<imagesID.length;i++){
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(imagesID[i]);
            imageViews.add(imageView);
            //初始化小灰点
            ImageView dot = new ImageView(this);
            dot.setImageResource(R.drawable.shape_point_gray);
            //初始化布局参数
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            if (i>0) {
                layoutParams.leftMargin = 25;
            }
            dot.setLayoutParams(layoutParams);
            lldots.addView(dot);
        }

    }

    class GuideViewPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = imageViews.get(position);
            container.addView(view);
            return view;

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);

        }
    }
}
